package services;

import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;

import t4p.model.User;
import t4p.persistence.DaoFactory;

public class UserService {
	private static UserService instance = new UserService();
	private UserService(){}
	
	public static UserService getInstace(){
		return instance;
	}
	
	public User login(String username, String password){
		User _return = null;
		Predicate userPredicate = new LoginUserPredicate(username, password);
		Collection<User> usersList = DaoFactory.getInstance().getUserDao().findAll();
		User loginUser = (User)CollectionUtils.find(usersList, userPredicate);
		if(loginUser != null){
			_return = loginUser;
		}
		return _return;
	}
	
	public User getUserByUsername(String username){
		User _return = null;
		Predicate userPredicate = new UserPredicate(username);
		Collection<User> usersList = DaoFactory.getInstance().getUserDao().findAll();
		User currentUser = (User)CollectionUtils.find(usersList, userPredicate);
		if(currentUser != null){
			_return = currentUser;
		}
		return _return;
	}
	
	public boolean addFollowing(User loggedUser, User following){
		boolean _return = false;
		
		Predicate userPredicate = new UserPredicate(following.getUsername());
		Collection<User> userFollowingsList = loggedUser.getFollowing();
		
		User newFollowingUser = (User)CollectionUtils.find(userFollowingsList, userPredicate);
		if(newFollowingUser == null){
			_return = loggedUser.getFollowing().add(following);	
		}
		return _return;
	}
	
	public boolean addFollower(User follower, User following){
		boolean _return = false;
		
		Predicate userPredicate = new UserPredicate(follower.getUsername());
		Collection<User> userFollowersList = following.getFollowers();
		
		User newFollowerUser = (User)CollectionUtils.find(userFollowersList, userPredicate);
		if(newFollowerUser == null){
			_return = following.getFollowers().add(follower);	
		}
		return _return;
	}
	
	public boolean removeFollowing(User loggedUser, User following){
		boolean _return = false;
		
		Predicate userPredicate = new UserPredicate(following.getUsername());
		Collection<User> userFollowingsList = loggedUser.getFollowing();
		
		User newFollowingUser = (User)CollectionUtils.find(userFollowingsList, userPredicate);
		if(newFollowingUser != null){
			_return = loggedUser.getFollowing().remove(following);
		}
		
		return _return;
	}
	
	public boolean removeFollower(User follower, User following){
		boolean _return = false;
		
		Predicate userPredicate = new UserPredicate(follower.getUsername());
		Collection<User> userFollowersList = following.getFollowers();
		
		User newFollowerUser = (User)CollectionUtils.find(userFollowersList, userPredicate);
		if(newFollowerUser != null){
			_return = following.getFollowers().remove(follower);	
		}
		return _return;
	}
	
	public boolean editAtributtes(User user, String userFullname, String userDescription){
		boolean _return = false;
		
		if(userFullname != null){
			user.setFullName(userFullname);
			_return = true;
		}
		
		if(userDescription != null){
			user.setDescription(userDescription);
			_return = true;
		}
		
		return _return;
	}
	
	public boolean isFollowing(User loggedUser, User following){
		boolean _return = false;
		
		Predicate userPredicate = new UserPredicate(following.getUsername());
		Collection<User> userFollowingsList = loggedUser.getFollowing();
		
		User newFollowingUser = (User)CollectionUtils.find(userFollowingsList, userPredicate);
		if(newFollowingUser != null){
			_return = true;	
		}
		return _return;
	}
}







